﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InformationSystem.BLL.Core.Model;
using InformationSystem.BLL.Core.Dao;

namespace InformationSystem.BLL.Core.Service.Impl
{
    public class ForumServiceImpl : IForumService
    {
        public IForumDao ForumDao { get; set; }
        public ICollegeDao CollegeDao { get; set; }

        public College GetCollegeForumInfo(int collegeID, LoginUser user)
        {
            College college = CollegeDao.GetCollegeInfo(collegeID);
            if (college == null) return null;
            if (college.ForumStatus == false || college.UserForum == false)
            {
                college.CurrentUserCanVisitForum = false;
                return college;
            }
            if (college.CollegeId != user.CollegeID && !college.ForumPublicToAll)
            {
                college.CurrentUserCanVisitForum = false;
                return college;
            }
            //Read Type
            college.ThreadTypeList = ForumDao.GetForumTypeList(collegeID);
            return college;
        }


        public IList<ThreadInfo> GetThreads(int collegeid, int threadtype, int pagesize, int requestpage, out int totalrecordcount)
        {
            return ForumDao.GetThreads(collegeid, threadtype, pagesize, requestpage, out totalrecordcount);
        }


        public bool CreateThread(ThreadInfo tinfo, LoginUser user)
        {
            //信息权限检查;
            College college = CollegeDao.GetCollegeInfo(tinfo.CollegeID);
            if (college == null || college.UserForum == false)
            {
                tinfo.Msg = "没有此论坛，你不能发帖";
                return false;
            }
            if (college.ForumStatus == false)
            {
                tinfo.Msg = "论坛整理中，你不能发帖";
                return false;
            }
            if (user.CollegeID != tinfo.CollegeID && !college.ForumPublicToAll)
            {
                tinfo.Msg = "此论坛不对其他学院开放，你不能发帖";
                return false;
            }

            return ForumDao.CreateThread(tinfo);

        }


        public bool DeleteThread(int threadid, LoginUser user, out  int forumid)
        {
            forumid = 0;
            ThreadInfo threadinfo = ForumDao.GetThreadDetail(threadid);
            if (threadinfo == null)
                return false;
            forumid = threadinfo.CollegeID;
            if (threadinfo.CreatUserID == user.UserID || user.LoginUserType == UserType.Assistant && user.CollegeID == threadinfo.CollegeID)
            {
                return ForumDao.DeleteThread(threadid);
            }
            return false;
        }

        public bool DeleteReply(int replyid, LoginUser user)
        {
            ThreadReplyInfo threadreplyinfo = ForumDao.GetThreadReplyDetail(replyid);
            if (threadreplyinfo == null) return false;
            ThreadInfo threadinfo = ForumDao.GetThreadDetail(threadreplyinfo.ThreadID);
            if (threadinfo == null) return false;
            if (threadinfo.CreatUserID == user.UserID || threadreplyinfo.ReplyUserID == user.UserID || user.LoginUserType == UserType.Assistant && user.CollegeID == threadinfo.CollegeID)
            {
                bool result= ForumDao.DeleteReply(replyid);
                ForumDao.UpdateReplyCount(threadreplyinfo.ThreadID);
                return result;
            }
            user.Msg = "你没有权限删除此帖！";
            return false;
        }

        public bool HiLightThread(int replyid, bool hilight, LoginUser user)
        {
            throw new NotImplementedException();
        }

        public bool CreateReply(int threadid, ThreadReplyInfo rinfo, LoginUser user)
        {
            //信息权限检查;
            ThreadInfo threadinfo = ForumDao.GetThreadDetail(threadid);
            College college = CollegeDao.GetCollegeInfo(threadinfo.CollegeID);
            if (college == null || college.UserForum == false)
            {
                rinfo.Msg = "没有此论坛，你不能回帖";
                return false;
            }
            if (college.ForumStatus == false)
            {
                rinfo.Msg = "论坛整理中，你不能回帖";
                return false;
            }
            if (user.CollegeID != threadinfo.CollegeID && !college.ForumPublicToAll)
            {
                rinfo.Msg = "此论坛不对其他学院开放，你不能回帖";
                return false;
            }
            if (!threadinfo.CanReply)
            {
                rinfo.Msg = "此帖子只读，你不能回帖";
                return false;
            }
            if (!threadinfo.CanReplyHideName) rinfo.HideName = false;
            ForumDao.CreateReply(threadid, rinfo);
            ForumDao.UpdateReplyCount(threadid);
            ForumDao.UpdateThreadLastReplyInfo(threadid, rinfo);
            return true;
        }

        public ThreadInfo GetThreadDetail(int threadid, LoginUser user)
        {
            //读取Thread信息
            ThreadInfo info = ForumDao.GetThreadDetail(threadid);
            if (info == null) return null;

            //根据college读取对应学院信息
            College college = CollegeDao.GetCollegeInfo(info.CollegeID);
            if (college == null) return null;
            if (college.ForumStatus = false || college.UserForum == false) return null;
            if (college.CollegeId != user.CollegeID && !college.IsPublicDept) return null;
            return info;
        }

        public bool UpdateReadCount(int threadid)
        {
            return ForumDao.UpdateReadCount(threadid);
        }

        //public bool UpdateReplyCount(int threadid)
        //{
        //    return ForumDao.UpdateReplyCount(threadid);
        //}


        public IList<ThreadReplyInfo> GetThreadReplyInfo(int threadid, int pagesize, int requestpage, out int totalrecordcount)
        {
            return ForumDao.GetThreadReplyInfo(threadid, pagesize, requestpage, out totalrecordcount);
        }


        public ThreadReplyInfo GetThreadReplyDetail(int replyid)
        {
            return ForumDao.GetThreadReplyDetail(replyid);
        }


        public int GetThreadReplysCount(int threadid)
        {
            return ForumDao.GetThreadReplysCount(threadid);
        }
    }
}
